<template>
  <div style="width: 100%; height: 100vh; overflow: hidden;" :style="backGround">

    <div style="width: 400px; left: 50%;
  top: 50%;    transform: translate(-50%, -50%); position: absolute; ">
      <el-form ref="form" :model="form" class="login-form" @keyup.enter="login()">
        <h2 v-if="this.form.role == 0" style="margin-bottom:15px">欢迎登录(学生端)</h2>
        <h2 v-if="this.form.role == 1" style="margin-bottom:15px">欢迎登录(管理员端)</h2>

        <el-form-item>
          <el-input v-if="this.form.role == 0" style="width: 80%;" placeholder="请输入学号" prefix-icon="el-icon-user-solid"
                    v-model="form.userId"></el-input>
          <el-input v-if="this.form.role == 1" style="width: 80%;" placeholder="请输入员工号" prefix-icon="el-icon-user-solid"
                    v-model="form.userId"></el-input>
        </el-form-item>

        <el-form-item>
          <el-input prefix-icon="el-icon-lock" placeholder="请输入密码" style="width: 80%;" v-model="form.password"
                    show-password></el-input>
        </el-form-item>

        <el-form-item>
          <el-button style="width: 80%;" type="primary" @click="login">登录
          </el-button>
        </el-form-item>

        <el-form-item prop="role" style="margin-top: 15px;">

          <el-radio-group v-model="radio" @change="roleChange" size="medium">
            <el-radio-button label="0">学生</el-radio-button>
            <el-radio-button label="1">管理员</el-radio-button>
          </el-radio-group>

        </el-form-item>

      </el-form>

    </div>

  </div>
</template>

<script>
import {ElMessage} from "element-plus";
import request from "../utils/request";

import backImg from "../assets/img.png"

export default {
  name: "Login",
  data() {
    return {
      radio: '0',
      form: {
        role: 0
      },

      backGround: {
        backgroundImage: "url(" + backImg + ")",
        backgroundRepeat: "no-repeat",
        backgroundSize: "100% 100%"
      }
    }
  },

  methods: {
    //当权限选择更改
    roleChange() {
      this.form.role = this.radio;
    },
    login() {
      console.log(this.form);
      request.post("/user/login", this.form).then(res => {
        if (res.code === '0') {
          //缓存登录用户
          sessionStorage.setItem("user", JSON.stringify(res.data))
          //登录成功后跳转到主页
          this.$router.push("/person");
        } else {
          ElMessage({
            message: res.msg,
            type: 'error'
          })
        }
      })

    },

  }
}
</script>

<style scoped>
.login-form {
  display: flex;
  position: relative;
  width: 450px;
  height: 350px;
  flex-direction: column;
  padding: 40px;
  text-align: center;
  z-index: 1;
  background: inherit;
  border-radius: 18px;
  overflow: hidden;
}

.login-form::before {
  content: '';
  position: absolute;
  top: -10px;
  left: -10px;
  width: calc(100% + 20px);
  /* + 两边各有一个空格  否则 无效*/
  height: calc(100% + 20px);
  background: inherit;
  box-shadow: 0 0 0 200px rgba(255, 255, 255, .2) inset;
  z-index: -1;
  filter: blur(6px);
  overflow: hidden;
}

.login-form h2 {
  font-size: 18px;
  font-weight: 400;
  color: #3d5245;
}

</style>